/**
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function (nums) {
  //最大子数组和maxSum，初始化为nums[0]
  let maxSum = nums[0]
  //当前最大子数组和currentSum，初始化为0
  let currentSum = 0
  //遍历数组元素
  for (let num of nums) {
    //如果 当前最大子数组和currentSum < 0，则丢弃前面的子序列（currentSum重置为0）
    if (currentSum < 0) currentSum = 0
    //更新当前最大子数组和currentSum
    currentSum += num
    //如果 当前最大子数组和currentSum > 最大子数组和maxSum，则用当前最大子数组和currentSum更新最大子数组和maxSum
    if (currentSum > maxSum) maxSum = currentSum
  }
  //返回最大子数组和maxSum
  return maxSum
}
console.log(maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4]))